<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文件组名" prop="fileGroupName">
        <el-input
          v-model="queryParams.fileGroupName"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测日期" prop="testdate">
        <el-date-picker clearable
          v-model="queryParams.testdate"
          type="date"
          :default-value="new Date()"
          value-format="YYYY-MM-DD"
          placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检测结果" prop="resultvalue">
        <el-select v-model="queryParams.resultvalue" placeholder="请输入成功1,失败0">
          <el-option value="1" label="成功"/>
          <el-option value="0" label="失败"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['operation:fileResult:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileResultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件组名" align="center" prop="fileGroupName" />
      <el-table-column label="检测日期" align="center" prop="testdate">
        <template #default="scope">
          <span>{{ parseTime(scope.row.testdate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测结果" align="center" prop="resultvalue">
        <template #default="scope">
          <el-tag v-if="scope.row.resultvalue == 1" type="success">成功</el-tag>
          <el-tag v-if="scope.row.resultvalue == 0" type="danger">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="resultmemo" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件检测结果对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="fileResultRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="" prop="filegroupid">
          <el-input v-model="form.filegroupid" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="testdate">
          <el-date-picker clearable
            v-model="form.testdate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="成功1,失败0" prop="resultvalue">
          <el-input v-model="form.resultvalue" placeholder="请输入成功1,失败0" />
        </el-form-item>
        <el-form-item label="2：完成；1：处理中；0：未处理" prop="resultmemo">
          <el-input v-model="form.resultmemo" placeholder="请输入2：完成；1：处理中；0：未处理" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FileResult">
import { listFileResult, getFileResult, delFileResult, addFileResult, updateFileResult } from "@/api/operation/fileResult";
import {parseTime} from "@/utils/ruoyi";

const { proxy } = getCurrentInstance();

const fileResultList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    testdate: parseTime(new Date(), "{y}-{m}-{d}"),
    resultvalue: undefined,
    resultmemo: undefined,
    queryParams: undefined
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    filegroupid: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    testdate: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    resultvalue: [
      { required: true, message: "成功1,失败0不能为空", trigger: "blur" }
    ],
    resultmemo: [
      { required: true, message: "2：完成；1：处理中；0：未处理不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询文件检测结果列表 */
function getList() {
  loading.value = true;
  listFileResult(queryParams.value).then(response => {
    fileResultList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    filegroupid: null,
    testdate: new Date(),
    resultvalue: null,
    resultmemo: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    fileGroupName: null
  };
  proxy.resetForm("fileResultRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加文件检测结果";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getFileResult(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改文件检测结果";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["fileResultRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateFileResult(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addFileResult(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除文件检测结果编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delFileResult(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/fileResult/export', {
    ...queryParams.value
  }, `fileResult_${new Date().getTime()}.xlsx`)
}

getList();
</script>
